// pages/debtorBaseInfo/branch/branch.ts

import { Debtor } from "../../../../../typings/types/debtor";
import { branchAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";

interface PageData {
  // page: number,
  companyId: string,
  // total: number | undefined,
  // items: Debtor.Branch[]
}

new ZCC.RefreshListPage<Debtor.Branch>({

  getBranch: async function () {

    this.setRefreshing()
    // wx.showLoading()
    try {
      console.log(this.data.page)
      const res = await branchAPI(this.data.companyId, this.data.page);
      if (res.content) {
        const list = res.content.list 
        console.log(res.content)
        this.updateDatasWithNewItems(list, res.content.total, res.content.hasNextPage)
        // if (list.length > 0) {

        //   const items = this.data.items.concat(list)
        //   const page = ++this.data.page
        //   this.setData({
        //     page: page,
        //     items: items,
        //     total: res.content.total || 0
        //   })
        // }else {
        //   this.setData({
        //     total: res.content.total || 0
        //   })
        // }

      }
    } finally {
      wx.hideLoading()
      this.endRefreshing()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    // page: 1,
    companyId: '',
    // total: undefined,
    // items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getBranch()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getBranch()
    } 
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getBranch()
  },

})